package com.study.springboot.controller;

import com.alibaba.nacos.shaded.com.google.common.collect.ImmutableMap;
import com.study.springboot.dto.Depart;
import com.study.springboot.service.DepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author huyong
 */
@RestController
@RequestMapping("api/v1/provicer/depart")
public class DepartController {

    @Autowired
    private DepartService departService;
    @Value("${server.port:8080}")
    private int serverPort;


    @PostMapping
    public boolean saveDepart(@RequestBody Depart depart) {
        return departService.saveDepart(depart);
    }

    @PutMapping
    public boolean modifyDepart(@RequestBody Depart depart) {
        return departService.modifyDepart(depart);
    }

    @DeleteMapping("/{id}")
    public boolean deleteDepartById(@PathVariable("id") Integer id) {
        return departService.deleteDepartById(id);
    }

    @GetMapping("/{id}")
    public ResponseEntity<Depart> getDepartById(@PathVariable("id") Integer id) {
        Depart depart = departService.getDepartById(id);
        return ResponseEntity.ok(depart);
    }

    @GetMapping(produces = "application/json")
    @ResponseBody
    public List<Depart> getAllDeparts() {
        return departService.getAllDeparts();
    }


    @GetMapping("/port")
    @ResponseBody
    public Map<String, Integer> getPort() {
        return ImmutableMap.of("port", serverPort);
    }


}
